package mark.wxming.twotigerlock.controller;

import mark.wxming.twotigerlock.lock.ILockHandler;
import mark.wxming.twotigerlock.lock.ILockIdentify;
import mark.wxming.twotigerlock.lock.LockFactory;
import mark.wxming.twotigerlock.lock.StringLockIdentify;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @author twotiger-wxm.
 * @date 2019/4/23.
 */
@RestController
@RequestMapping("/webReq/")
public class WebReqController {
    private static final Logger LOGGER = Logger.getLogger(WebReqController.class);

    // 活动加锁
    //此处锁用的是redis锁还是内存锁，会在锁工厂中根据配置判断赋值。
    private static final ILockHandler lock = LockFactory.otherLockHandler;

    @RequestMapping("test")
    public String test(){
        ILockIdentify identify = new StringLockIdentify("yonghu1"+"shangpin1");
        Random rand = new Random();
        int num = rand.nextInt(1000);
        try {
            LOGGER.info("=========="+num+"==========请求来了");
            lock.lock(identify);
            LOGGER.info("=========="+num+"==========加锁后执行业务代码");
        } finally {
            LOGGER.info("=========="+num+"==========去解锁了");
            lock.unLock(identify);
            LOGGER.info("=========="+num+"==========解锁成功");
        }
        return "哈哈哈";
    }
}
